package com.whfc.ms.api.mach.service.impl;

import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.util.PinyinUtil;
import com.whfc.fuum.dto.SysMachListDTO;
import com.whfc.fuum.dto.SysMachTypeDTO;
import com.whfc.fuum.param.*;
import com.whfc.fuum.service.SysMachTypeService;
import com.whfc.ms.api.mach.service.MsMachTypeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.util.List;

/**
 * @Description 系统管理-设备类型接口实现类
 * @Author hw
 * @Date 2020/8/24 20:30
 * @Version 1.0
 */
@Service
public class MsMachTypeServiceImpl implements MsMachTypeService {

    @DubboReference(interfaceClass = SysMachTypeService.class, version = "1.0.0")
    private SysMachTypeService sysMachTypeService;

    /**
     * 设备类型图标图片路径
     */
    private String machTypeIconPath = "sys/mach-type-icon";

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private FilePathConfig filePathConfig;

    @Override
    public void machTypeAdd(SysMachTypeAddParam param) {
        uploadImg(param.getIcons(), param.getName());
        sysMachTypeService.machTypeAdd(param);
    }

    @Override
    public void machTypeClassifyAdd(SysMachTypeClassifyAddParam param) {
        sysMachTypeService.machTypeClassifyAdd(param);
    }

    @Override
    public void machTypeEdit(SysMachTypeEditParam param) {
        uploadImg(param.getIcons(), param.getName());
        sysMachTypeService.machTypeEdit(param);
    }

    @Override
    public PageData<SysMachTypeDTO> machTypeList(Integer machTypeId, Integer pageNum, Integer pageSize, String keyword,Integer state) {
        return sysMachTypeService.machTypeList(machTypeId, pageNum, pageSize, keyword,state);
    }

    @Override
    public void machTypeClassifyEdit(SysMachTypeClassifyEditParam param) {
        sysMachTypeService.machTypeClassifyEdit(param);
    }

    @Override
    public ListData<SysMachTypeDTO> machTypeClassifyList(String keyword) {
        return sysMachTypeService.machTypeList(keyword);
    }

    @Override
    public void machTypeDel(Integer machTypeId) {
        sysMachTypeService.machTypeDel(machTypeId);
    }

    @Override
    public void enable(Integer machTypeId, Integer state) {
        sysMachTypeService.enable(machTypeId, state);
    }

    @Override
    public ListData<SysMachListDTO> machLists(Integer machTypeId) {
        return sysMachTypeService.machLists(machTypeId);
    }

    private void uploadImg(List<SysMachTypeIconAddParam> icons, String name) {
        for (SysMachTypeIconAddParam sysMachTypeIconAddParam : icons) {
            if (!StringUtils.isEmpty(sysMachTypeIconAddParam.getIcon()) && !sysMachTypeIconAddParam.getIcon().startsWith("http")) {
                //上传图片
                byte[] imageByte = Base64.decodeBase64(sysMachTypeIconAddParam.getIcon());
                ByteArrayInputStream inputStream = new ByteArrayInputStream(imageByte);
                String fileName = String.format("%s_%s.png", PinyinUtil.toPinyin(name).toLowerCase(), sysMachTypeIconAddParam.getState());
                String ossKey = filePathConfig.getFilePath(machTypeIconPath, fileName);
                String url = fileHandler.upload(ossKey, inputStream);
                sysMachTypeIconAddParam.setIcon(url);
            }
        }
    }
}
